package cn.smbms.controller;

import cn.smbms.pojo.User;
import cn.smbms.service.user.UserService;
import cn.smbms.tools.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.smbms.service.user.UserService;
import cn.smbms.service.user.UserServiceImpl;
import org.springframework.web.bind.annotation.RequestParam;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class LoginController {

    @Resource
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = {"/login.html","/"},method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    @RequestMapping(value = "/dologin.html",method=RequestMethod.GET)
    public String doLogin(@RequestParam String userCode,
                          @RequestParam String userPassword,
                          HttpServletRequest request,
                          HttpSession session)throws Exception{
        //调用service方法，进行用户匹配
        User user = userService.login(userCode,userPassword);
        if (null != user){
            session.setAttribute(Constants.USER_SESSION,user);
            return "redirect:/sys/main.html";
        }else{
            request.setAttribute("error","用户名或密码不正确");
            return "login";
        }
    }

    @RequestMapping(value = "/logout.html")
    public String logout(HttpSession session){
        session.removeAttribute(Constants.USER_SESSION);
        return "login";
    }

    @RequestMapping(value = "/sys/main.html")
    public String main(){
        return "frame";
    }
//    @RequestMapping(value = "/login.html",method = RequestMethod.POST)
//    public String login(String userCode, String userPassword, Model model, HttpSession session)throws Exception{
//        User user = userService.selectUserCodeExist(userCode);
//
//        if (user != null){
//            user = userService.login(userCode,userPassword);
//            if (user == null){
//                model.addAttribute("error","密码输入错误！");
//                return "login";
//            }
//            session.setAttribute(Constants.USER_SESSION,user);
//            return "redirect:sys/main.html";
//        }
//        model.addAttribute("error","用户名不存在！");
//        return "login";
//    }
//
//    @RequestMapping(value = "/sys/main.html")
//    public String main(){
//        return "frame";
//    }
//
//    @RequestMapping("logout.html")
//    public String loginout(HttpSession httpSession){
//        httpSession.removeAttribute(Constants.USER_SESSION);
//        return "login";
//    }


}
